<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page language="java" import="java.sql.*" %>
<%@ page language="java" import="java.util.ArrayList" %>
<%@ page language="java" import="java.util.HashSet" %>
<%@ page language="java" import="java.util.LinkedList" %>
<%@ page language="java" import="java.util.Date"%>
<%@ page language="java" import="db.Config" %>
<%@ page language="java" import="db.Activity" %>
<%@ page language="java" import="db.Change" %>
<%-- -------- Open Connection Code -------- --%>
<%
  if (application.getAttribute("activeUsers") == null) {
    application.setAttribute("activeUsers", new HashSet<String> ());
  }

  String uuid = request.getParameter("uuid");
  String action = request.getParameter("action");

  System.out.println("action: " + action);
  if (uuid == null) {
    System.out.println("unknown uuid!");
  }

	Connection conn = null;
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	try {
    // Load JDBC Driver class file
    Class.forName(Config.jdbcDriver);

    if (action != null && action.equals("initRequest")) {
      HashSet<String> activeUsers = (HashSet<String>) application.getAttribute("activeUsers");
      activeUsers.add(uuid);
      application.setAttribute(uuid, new Activity());

      System.out.println("new user: " + uuid);

      // Open a connection to the database using DriverManager
      conn = DriverManager.getConnection(Config.connectionURL, Config.username, Config.password);

      pstmt = conn.prepareStatement(
        "select u.state state, c.name category, SUM(o.amount) sales " +
        "FROM user u, product p, category c, `order` o " + 
        "WHERE p.id = o.product AND p.category = c.id " + 
        "AND o.user = u.id AND o.state='done' and o.`date` = ? " + 
        "GROUP BY u.state, c.name ORDER BY u.state, c.name");

      Date date = new Date();
      pstmt.setDate(1, new java.sql.Date(date.getTime()));
      rs = pstmt.executeQuery();

      out.println("[");
      boolean first = true;
      while (rs.next()) {
        if (first)
          first = false;
        else
          out.println(",");
        out.print("{\"state\" : \"" + rs.getString("state") + "\", \"category\" : \"" + rs.getString("category") + "\", \"sales\" : " + rs.getInt("sales") + "}");
      }
      out.println("\n]");

      rs.close();

      pstmt.close();

      conn.close();
    } else if (action != null && action.equals("update")) {

      System.out.println(uuid);

      Activity activity = (Activity) application.getAttribute(uuid);

      activity.lastVisited = new Date();

      LinkedList<Change> updates = activity.updates;
      out.println("[");
      boolean first = true;
      while (!updates.isEmpty()) {
        if (first)
          first = false;
        else
          out.println(",");
        out.print(updates.remove().toString());
      }
      out.println("\n]");
    }
%>
<%-- -------- Close Connection Code -------- --%>
<%
  } catch (SQLException e) {

    out.println(e.getMessage());

  } finally {
    // Release resources in a finally block in reverse-order of
    // their creation
    if (rs != null) {
      try {
        rs.close();
      } catch (SQLException e) { } // Ignore
      rs = null;
    }
    if (pstmt != null) {
      try {
        pstmt.close();
      } catch (SQLException e) { } // Ignore
      pstmt = null;
    }
    if (conn != null) {
      try {
        conn.close();
      } catch (SQLException e) { } // Ignore
      conn = null;
    }
  }
%>
